package com.ruoyi.web.controller.module.setMeal;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.setMeal.SetMeal;
import com.ruoyi.system.domain.module.setMeal.SetMealInfo;
import com.ruoyi.system.domain.module.setMeal.SetMealOrder;
import com.ruoyi.system.domain.module.wx.WxUser;
import com.ruoyi.system.service.module.setMeal.ISetMealInfoService;
import com.ruoyi.system.service.module.setMeal.ISetMealService;
import com.ruoyi.system.service.module.wx.IWxUserService;
import com.ruoyi.web.controller.common.BaseController;
import com.vdurmont.emoji.EmojiParser;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.setMeal.ISetMealOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 套餐活动支付订单Controller
 *
 * @author ruoyi
 * @date 2022-06-06
 */
@Validated
@Api(value = "套餐活动支付订单控制器", tags = {"套餐活动支付订单管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/meal/order")
public class SetMealOrderController extends BaseController {

    private final ISetMealOrderService setMealOrderService;
	private final ISetMealService setMealService;
	private final ISetMealInfoService setMealInfoService;
    private final IWxUserService wxUserService;
	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

    /**
     * 查询套餐活动支付订单列表
     */
    @ApiOperation("查询套餐活动支付订单列表")
    @PreAuthorize("@ss.hasPermi('module:mealOrder:list')")
    @GetMapping("/list")
    public TableDataInfo<SetMealOrder> list(@Validated SetMealOrder setMealOrder) {
        QueryWrapper<SetMealOrder> qw = new QueryWrapper<>();
		SysUser sysUser = getUser();
		if(sysUser.getUserType() == 2){//物业公司
			qw.eq("property_id",sysUser.getPropertyId());
		} else if(sysUser.getUserType() == 3){//小区
			qw.eq("estate_id",sysUser.getEstateId());
		}else{
			if(setMealOrder.getEstateId()!=null){
				qw.eq("estate_id",setMealOrder.getEstateId());
			}
		}
        if(setMealOrder.getStatus() !=null) {//物业公司
            qw.eq("status",setMealOrder.getStatus());
        }
		if(StrUtil.isNotEmpty(setMealOrder.getOrderNo())){
			qw.like("order_no",setMealOrder.getOrderNo());
		}
        if(StrUtil.isNotEmpty(setMealOrder.getNickName())){
        	qw.like("nick_name",setMealOrder.getNickName());
		}
		if(StrUtil.isNotEmpty(setMealOrder.getBeginDate())){
			qw.between("create_time",setMealOrder.getBeginDate() + " 00:00:00",setMealOrder.getBeginDate() + " 23:59:59");
		}
        qw.orderByDesc("create_time");

        TableDataInfo<SetMealOrder> td = PageUtils.buildDataInfo(setMealOrderService.page(PageUtils.buildPage(),qw));
        List<SetMealOrder> list = td.getRows();
        if(CollUtil.isNotEmpty(list)){
        	for (SetMealOrder order  : list){
				SetMeal setMeal = setMealService.findById(order.getMealId());
				if(setMeal!=null){
					order.setMealTitle(setMeal.getMealName());
				}

                WxUser wxUser = wxUserService.findById(order.getUserId());
				if(wxUser!=null){
					if(StrUtil.isNotEmpty(wxUser.getFaceUrl()) && !wxUser.getFaceUrl().contains("http")){
						order.setFaceUrl(imgUrl + wxUser.getFaceUrl());
					}else{
						order.setFaceUrl(wxUser.getFaceUrl());
					}
                }

				SetMealInfo setMealInfo = setMealInfoService.findById(order.getMealInfoId());
				if(setMealInfo!=null){
					order.setMealInfoTitle(setMealInfo.getMealInfoName());
				}

				if(StrUtil.isNotEmpty(order.getNickName())){
                    order.setNickName(EmojiParser.parseToUnicode(order.getNickName()));
                }
			}
		}
        return td;
    }

    /**
     * 导出套餐活动支付订单列表
     */
    @ApiOperation("导出套餐活动支付订单列表")
    @PreAuthorize("@ss.hasPermi('module:mealOrder:export')")
    @Log(title = "套餐活动支付订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<SetMealOrder> export(@Validated SetMealOrder setMealOrder) {
        List<SetMealOrder> list = setMealOrderService.list();
        ExcelUtil<SetMealOrder> util = new ExcelUtil<SetMealOrder>(SetMealOrder.class);
        return util.exportExcel(list, "套餐活动支付订单");
    }

    /**
     * 获取套餐活动支付订单详细信息
     */
    @ApiOperation("获取套餐活动支付订单详细信息")
    @PreAuthorize("@ss.hasPermi('module:mealOrder:query')")
    @GetMapping("/{id}")
    public AjaxResult<SetMealOrder> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") String id) {
        SetMealOrder setMealOrder = setMealOrderService.getById(id);
        if(setMealOrder!=null){
			SetMeal setMeal = setMealService.findById(setMealOrder.getMealId());
			if(setMeal!=null){
				setMealOrder.setMealTitle(setMeal.getMealName());
			}

			SetMealInfo setMealInfo = setMealInfoService.findById(setMealOrder.getMealInfoId());
			if(setMealInfo!=null){
				setMealOrder.setMealInfoTitle(setMealInfo.getMealInfoName());
			}
		}
        return AjaxResult.success(setMealOrder);
    }

    /**
     * 新增套餐活动支付订单
     */
    @ApiOperation("新增套餐活动支付订单")
    @PreAuthorize("@ss.hasPermi('module:mealOrder:add')")
    @Log(title = "套餐活动支付订单", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody SetMealOrder setMealOrder) {
        return toAjax(setMealOrderService.save(setMealOrder) ? 1 : 0);
    }

    /**
     * 修改套餐活动支付订单
     */
    @ApiOperation("修改套餐活动支付订单")
    @PreAuthorize("@ss.hasPermi('module:mealOrder:edit')")
    @Log(title = "套餐活动支付订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody SetMealOrder setMealOrder) {

    	if(setMealOrder.getRewardVerify() == 1){
			setMealOrder.setVerifyTime(new Date());
		}

    	boolean bl = setMealOrderService.updateById(setMealOrder);

        return toAjax(bl ? 1 : 0);
    }

    /**
     * 删除套餐活动支付订单
     */
    @ApiOperation("删除套餐活动支付订单")
    @PreAuthorize("@ss.hasPermi('module:mealOrder:remove')")
    @Log(title = "套餐活动支付订单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable String[] ids) {
        return toAjax(setMealOrderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
